package com.ag.zhh.dao;

import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ag.zhh.vo.Contact;
import com.ag.zhh.vo.UserDept;
import com.ag.zhh.vo.UserDeptContact;

public class UserDeptContactDAO extends HibernateDaoSupport{
	
private static final Log log = LogFactory.getLog(ContactDAO.class);
	
	public void saveOrUpdateContact(UserDeptContact user) {
		log.debug("saveOrUpdateUserDeptContact contact!");
		this.getHibernateTemplate().saveOrUpdate(user);
	}
	
	public void deleteUserDeptContact(UserDeptContact user) {
		log.debug("deleteContact contact!");
		this.getHibernateTemplate().delete(user);
	}
	/**
	 * 根据用户通信录的id查询
	 * @param CId
	 * @return
	 */
	public UserDeptContact findById(String CId) {
		log.debug("findContact byCId!");
		return (UserDeptContact)this.getHibernateTemplate().get(UserDeptContact.class, CId);
		
	}
	/**
	 * 根据部门id查询通信录
	 * @param deptId
	 * @return
	 */
	public List<UserDeptContact> findByDeptId(String deptId)
	{
		List<UserDeptContact> result=null;
		Session session = this.getSessionFactory().openSession();
		result = session.createSQLQuery("select * from TB_USER user left outer join TB_USERDEPT_USER dept_contact on user.U_ID = dept_contact.C_ID where dept_contact.UD_ID = :did").addEntity(Contact.class).setString("did", deptId).list();
		if(result!=null)
		//System.out.println("ContactDAO： OA部门："+deptId+" 通信录数目："+result.size());
		
		session.close();
		return result;
		
	}
	/**
	 * 查询所有的通信录
	 * @return
	 */
	public List<UserDeptContact> findAll()
	{
		List<UserDeptContact> result=null;
		Session session = this.getSessionFactory().openSession();
		result = session.createQuery("from UserDeptContact").list();
		session.close();
		return null;
		
	}
	//根据联系人姓名查找联系人
	public List<UserDeptContact> findContactByName(String name)
	{
		
	//	return getHibernateTemplate().findByNamedParam("from UserDeptContact c where c.UName like :cname", "cname","%"+name+"%");
		return this.getHibernateTemplate().findByNamedParam("from UserDeptContact user where user.UName =:uname", "uname", name);
	}
	
	public UserDeptContact merge(UserDeptContact detachedInstance) {
		log.debug("merging TbEventPlan instance");
		try {
			UserDeptContact result = (UserDeptContact) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
	
	public void refreshBelongDept(UserDeptContact user,Set<UserDept> userDepts){
		Session session = null;
		try{
			session = this.getSession(true);
			session.merge(user);
			
			for(UserDept dept:user.getTbUserDepts()) {
				logger.info("for循环------remove"+ dept.getUdId());
				
				dept.getTbUsers().remove(user);
				session.merge(dept);
			}
			//user.getTbUserDepts().removeAll(user.getTbUserDepts());
			session.merge(user);
			session.flush();
			session.clear();
			
			for (UserDept dept : userDepts) {
				logger.info("for循环------add"+ dept.getUdId());
				//session.merge(dept);
				//dept.getTbUsers().add(user);
				//session.refresh(dept);
			
				//session.refresh(user);
				dept.getTbUsers().add(user);
				session.merge(dept);
				session.flush();
				session.clear();
			}
			//user.getTbUserDepts().addAll(userDepts);
			//session.merge(user);
			session.flush();
			session.clear();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(session != null){
				session.close();
			}
		}
	}
	
}
